; (function (window, $) {
  $('#getCode').on('click', function () {
    if ($('.phone').val() == '') {
      layer.alert('请输入手机号码', { skin: 'demo-class', closeBtn: 0 })
      // alert('请输入手机号码')
      return
    }
    var phone = $('.phone').val()
    util.ajax({
      url: '/auth/checkValidPhone/' + phone,
      type: 'POST',
      success: function (res) {
        console.log(res)
        if (res.msg == '手机号已存在') {
          sendCode(phone)
        } else {
          layer.alert('该手机号码尚未注册', {
            skin: 'demo-class',
            closeBtn: 0
          })
          // alert('该手机号码尚未注册')
          return
        }
      },
      error: function (res) {
        console.log(res)
      }
    })
  })
  //click btn
  $('.pwdbtn').on('click', function () {
    var pwdObj = {}
    var bool = true
    $('input[type="text"].pwd').each(function () {
      if ($(this).val() === '') {
        layer.alert('请填写' + $(this).data('label'), {
          skin: 'demo-class',
          closeBtn: 0
        })
        // alert('请填写' + $(this).data('label'))
        bool = false
      }
      var value = $(this).val()
      var key = $(this).attr('name')
      pwdObj[key] = value
    })
    pwdObj.deviceId = $('.phone').val()
    console.log(pwdObj)

    var newpwd = $('input[type="text"].newpwd').val()
    if (newpwd.length < 6) {
      layer.alert('密码至少6位数', { skin: 'demo-class', closeBtn: 0 })
      bool = false
    }
    var confirm = $('input[type="text"].confirmpwd').val()
    if (newpwd !== confirm) {
      layer.alert('两次密码不一致！', { skin: 'demo-class', closeBtn: 0 })
      // alert('两次密码不一致！')
      bool = false
    }
    if (bool === false) {
      return
    }
    var phone = pwdObj.phone
    var deviceId = pwdObj.deviceId
    var smsCode = pwdObj.smsCode
    var newPassword = pwdObj.newPassword
    var confirmPwd = pwdObj.confirmPwd
    util.ajax({
      url:
        '/auth/resetLoginPwd?phone=' +
        phone +
        '&deviceId=' +
        deviceId +
        '&smsCode=' +
        smsCode +
        '&newPassword=' +
        newPassword +
        '&confirmPwd=' +
        confirmPwd,
      type: 'post',
      success: function (res) {
        console.log(res)
        layer.alert(
          '修改密码成功',
          {
            skin: 'demo-class',
            closeBtn: 0
          },
          function (index) {
            window.location.href = "/login.html"
          }
        )

      }
    })
  })

  function sendCode(phone) {
    util.ajax({
      url: '/auth/code/sms',
      type: 'get',
      data: {
        deviceId: phone,
        phone: phone
      },
      success: function (res) {
        layer.alert(JSON.parse(res).msg, { skin: 'demo-class', closeBtn: 0 })
      }
    })
  }
})(window, jQuery)
